<template>
  <div>
    <el-button @click="show_dialog">新增</el-button>
    <el-dialog :title="title" :visible.sync="dialog_hos" width="80%">
      <!-- {{form}} -->
      <el-form :model="form_hos" :rules="rules" class="demo-ruleForm">
        <el-form-item label="医院名称" :label-width="formLabelWidth_hos" prop="name">
          <el-input v-model="form_hos.name"></el-input>
        </el-form-item>

        <el-form-item label="医院简介" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.introduce"></el-input>
        </el-form-item>
        <el-form-item label="省市区" :label-width="formLabelWidth_hos">
          <el-cascader
            v-model="form_hos.pca"
            :options="pca"
            :props="{ expandTrigger: 'hover', label: 'name', value: 'name' }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" :label-width="formLabelWidth_hos" prop="address">
          <el-input v-model="form_hos.address"></el-input>
        </el-form-item>
        <el-form-item label="标记" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.flags"></el-input>
        </el-form-item>
        <el-form-item label="经度" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.longitude" disabled></el-input>
        </el-form-item>
        <el-form-item label="纬度" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.latitude" disabled></el-input>
        </el-form-item>
        <el-form-item label="选择经纬度" :label-width="formLabelWidth_hos">
          <el-button @click="show_map">打开地图</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_hos = false">取 消</el-button>
        <el-button type="primary" @click="save_hos()">发 布</el-button>
      </div>
    </el-dialog>
    <el-dialog title="选择地点" :visible.sync="dialog_map" width="80%">
      <div id="Map" style="height: 400px"></div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialog_map = false">确定</el-button>
      </div>
    </el-dialog>
    <el-table :data="hos.list" style="width: 100%">
      <el-table-column align="center" prop="name" label="医院名称">
      </el-table-column>
      <el-table-column
        align="center"
        prop="introduce"
        label="医院简介"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="address"
        label="地址"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="flags"
        label="标记"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="longitude"
        label="经度"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="latitude"
        label="纬度"
      ></el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button @click="edit_hos(scope.row)">修改</el-button>
          <el-button @click="delete_hos(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页按钮 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="obj.pageSize"
      :current-page="obj.page"
      @current-change="change"
      :total="hos.total"
    >
    </el-pagination>
  </div>
</template>
<script>
import { mapActions, mapState } from "vuex";
import pca from "../../assets/pca.json";
export default {
  computed: {
    ...mapState("hos", ["hos"]),
  },
  data() {
    var checkname = (rule, value, callback) => {
      console.log(value);
      if (!value) {
        return callback(new Error("医院名称不能为空"));
      }
    };
    var checkaddress = (rule, value, callback) => {
      console.log(value);
      if (!value) {
        return callback(new Error("详细地址不能为空"));
      }
    };
    return {
      pca,
      obj: {
        page: 1,
        pageSize: 10,
      },
      dialog_hos: false,
      dialog_map: false,
      title: "",
      form_hos: {},
      formLabelWidth_hos: "80",
      map: {},
      rules:{
        name: [
             { validator: checkname, trigger: 'blur' }
          ],
        address: [
             { validator: checkaddress, trigger: 'blur' }
          ],
      }
    };
  },
  created() {
    this.findHos(this.obj);
  },
  watch: {
    obj: {
      handler: function () {
        this.findHos(this.obj);
      },
      deep: true, //表示深度监听
    },
  },
  methods: {
    initMap() {
      this.map = new AMap.Map("Map", {
        resizeEnable: true,
      });
    },
    //获取经纬度
    showInfoClick(e) {
      this.form_hos.longitude = e.lnglat.getLng();
      this.form_hos.latitude = e.lnglat.getLat();
    },
    //给地图绑定单击事件
    clickOn() {
      this.map.on("click", this.showInfoClick);
    },
    ...mapActions("hos", ["findHos", "saveHos","DelHos"]),
    edit_hos(val) {
      this.title = "修改数据"
      this.dialog_hos = true;
      this.form_hos = val;
    },
    // 删除定点医院信息
    delete_hos(id) {
      var obj1 = {
        id: id,
      };
      this.$confirm('是否删除该医院信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.DelHos(obj1).then((res)=>{
            if (res.status == 200) {
              this.$notify({
                title: "成功",
                message: "删除成功",
                type: "success",
              })
            }
            this.findHos(this.obj)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        this.findHos(this.obj)      
      })
    },
    show_map() {
      this.dialog_map = true;
      setTimeout(() => {
        this.initMap();
        this.clickOn();
      }, 1000);
    },
    change(page) {
      this.obj.page = page;
    },
    //显示模态框
    show_dialog() {
      this.form_hos= {};
      this.dialog_hos = true;
    },
    handleChange() {},
    // 上传定点医院信息
    save_hos() {
      if (this.form_hos.pca) {
        this.form_hos.province = this.form_hos.pca[0];
        this.form_hos.city = this.form_hos.pca[1];
        this.form_hos.area = this.form_hos.pca[2];
      }
      this.saveHos(this.form_hos).then((res) => {
        
        if (res.status == 200) {
          this.$notify({
            title: "成功",
            message: "发布成功",
            type: "success",
           
          }); 
          this.dialog_hos = false;
          //清空模态框
          this.form_hos = {};
        } else {
          this.$notify.error({
            title: "错误",
            message: res.data.message,
          });
        }
        this.findHos(this.obj);
      });
    },
  },
};
</script>